<?php
/**
 * +-------------------------------------------------------------
 * |Caption:
 * +-------------------------------------------------------------
 * |Author: Lenny
 * +-------------------------------------------------------------
 * |Email:  1685964109@qq.com
 * +-------------------------------------------------------------
 * |Date:   2024/6/29 上午10:42
 * +-------------------------------------------------------------
 */

namespace app\admin\controller;

use app\admin\model\SystemLog;
use app\common\controller\AdminController;
use think\App;
use think\db\exception\DbException;
use think\db\exception\PDOException;
use think\response\Json;
use app\admin\service\annotation\ControllerAnnotation;
use app\admin\service\annotation\NodeAnnotation;

/**
 * @ControllerAnnotation (title="日志管理")
 */
class Log extends AdminController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new SystemLog();
    }

    /**
     * @NodeAnnotation (title="日志列表")
     * @return Json
     */
    public function list(): Json
    {
        [$page, $limit, $where, $excludeFields] = $this->buildSearchParams(['month']);
        $month = !empty($excludeFields['month']) ? date('Ym', strtotime($excludeFields['month'])) : date('Ym');
        $model = $this->model->setMonth($month)->with('admin')->where($where);
        try {
            $count = $model->count();
            $list  = $model->page($page, $limit)->order($this->sort)->select()->toArray();
        }catch (PDOException|DbException $exception) {
            $count = 0;
            $list  = [];
        }
        return $this->successJson(compact('count','list'));
    }
}